package com.zhkj.lib.base

import android.content.res.Resources
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import com.zhkj.lib.utils.ToastUtil
import com.zhkj.lib.widget.LoadingStatuesDialog
import com.tbruyelle.rxpermissions2.RxPermissions
import com.zhkj.lib.R
import io.reactivex.disposables.Disposable

abstract class BaseActivity : AppCompatActivity(), BaseView {

    init {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
    }

    private var permissionDisposable: Disposable? = null
    protected var loadingStatusDialog: LoadingStatuesDialog? = null

    /**
     * 需要绑定的视图ID
     */
    abstract val layoutResId: Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layoutResId)

        // 设置沉浸式状态栏
        initStatusBar()

        doBeforeInitUI()
        initUI()
        initData()
    }

    override fun onDestroy() {
        super.onDestroy()
        if (permissionDisposable != null && !permissionDisposable!!.isDisposed) {
            permissionDisposable!!.dispose()
        }
    }

    override fun getResources(): Resources {
        val res = super.getResources()
        if (res != null) {
            val config = res.configuration
            if (config != null && config.fontScale != 1.0f) {
                config.fontScale = 1.0f
                res.updateConfiguration(config, res.displayMetrics)
            }
        }
        return res
    }

    override fun requestPermission(permission: String, onGrant: () -> Unit, onDeny: () -> Unit) {
        if (RxPermissions(this).isGranted(permission)) {
            onGrant()
        } else {
            permissionDisposable = RxPermissions(this).request(permission).subscribe {
                if (it) onGrant() else onDeny()
            }
        }
    }

    override fun toast(content: String) {
        ToastUtil.show(content)
    }

    override fun showLoadingView(content: String?) {
        if (loadingStatusDialog == null) loadingStatusDialog = LoadingStatuesDialog(this)
        if (loadingStatusDialog?.isShowing == false) loadingStatusDialog?.showLoading(content)
    }

    override fun hideLoadingView() {
        loadingStatusDialog?.dismiss()
    }

    /**
     * 初始化数据
     */
    open fun initData() {
    }

    /**
     * 初始化视图
     */
    open fun initUI() {
    }

    /**
     * 在初始化视图和数据之前进行操作
     */
    open fun doBeforeInitUI() {
    }

    open fun statusTitleBar(): View? = null

    private fun initStatusBar() {
        var uiFlag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_VISIBLE
        // 清除标识
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//        //需要设置这个才能设置状态栏和导航栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        // 设置刘海屏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
            window.attributes.layoutInDisplayCutoutMode =
                WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
        // 设置状态栏文字颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            uiFlag = uiFlag or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        window.decorView.systemUiVisibility = uiFlag
        window.statusBarColor = Color.TRANSPARENT
        statusTitleBar()?.fitsSystemWindows = true
    }
}
